using IPS.SP2013.DiagnosticsProvider;
using IPS.SP2013.Helper;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System;
using System.Reflection;
using System.Runtime.InteropServices;

namespace IPS.SP2013.CustomLoginPage
{

    /// <summary>
    /// Event receiver asociated to "IPS Custom Login Page" feature
    /// </summary>
    [Guid("07b326cb-de7c-4b7c-b5bf-970774096802")]
    public class CustomLoginEventReceiver : SPFeatureReceiver
    {

        #region Override Methods

        /// <summary>
        /// Overrides "FeatureActivated" event: set custom login page at web application level
        /// </summary>
        /// <param name="properties">common event receiver properties</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Infomation,
                                        "Activating Feature: IPS.SP2013.CustomLoginPage (Scope: WebApplication)");

            try
            {
                this.ChangeLoginPage(Constants.CustomPageType.Custom, ref properties);
            }
            catch (Exception ex)
            {
                LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Error,
                                        "Error Activating Feature: IPS.SP2013.CustomLoginPage (Scope: WebApplication) - " + ex.Message);
                return;
            }

            LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Success,
                                        "Feature Activated: IPS.SP2013.CustomLoginPage (Scope: WebApplication)");
        }

        /// <summary>
        /// Overrides "FeactureDeactivating" event: set default login page at web application level
        /// </summary>
        /// <param name="properties">common event receiver properties</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Infomation,
                                      "Deactivating Feature: IPS.SP2013.CustomLoginPage (Scope: WebApplication)");

            try
            {
                this.ChangeLoginPage(Constants.CustomPageType.Default, ref properties);
            }
            catch (Exception ex)
            {
                LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Error,
                                          "Error Deactivating Feature: IPS.SP2013.CustomLoginPage (Scope: WebApplication) - " + ex.Message);
                return;
            }

            LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Success,
                                      "Feature Deactivated: IPS.SP2013.CustomLoginPage (Scope: WebApplication)");
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Private method to change login page at web application level
        /// </summary>
        /// <param name="type">Enum to indicate if the change is to set a custom or default login page</param>
        /// <param name="properties">common event receiver properties</param>
        private void ChangeLoginPage(Constants.CustomPageType type, ref SPFeatureReceiverProperties properties)
        {
            LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Infomation,
                            "IPSLogin.ChangeLoginPage(): Start");

            try
            {
                if (properties.Feature.Parent != null)
                {
                    // Get current web application
                    SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

                    if (type == Constants.CustomPageType.Custom)
                    {
                        // Set custom login page
                        IPSWebAppHelper.SetCustomLoginPage(ref webApp, SPUrlZone.Default, relativeLoginUrl: Constants.URL_IPSLogin);
                    }
                    else if (type == Constants.CustomPageType.Default)
                    {
                        // Set default login page
                        IPSWebAppHelper.SetCustomLoginPage(ref webApp, SPUrlZone.Default, defaultLoginPage: true);
                    }
                }
                else
                {
                    LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Warning,
                                              "Unable to get feature parent object");
                }
            }
            catch (Exception ex)
            {
                LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Error,
                                          "CustomLoginEventReceiver.ChangeLoginPage(): " + ex.Message);
                throw;
            }

            LogProvider.WriteLogEntry(DiagnosticsProvider.Constants.CategoryNames.Success,
                            "IPSLogin.ChangeLoginPage(): End");
        }

        #endregion
       
    }

}
